<?php

namespace app\common\model;
/**
 * [Message  消息表]
 * @author [C。]
 * @return html
 */
class Message extends Base
{
    protected $name = 'message';
    /**
     * 根据搜索条件获取列表信息
     */
    public function getlists($id,$from = 0, $size = 5)
    {
        $result = $this->query("select * from app_message where id in (select max(id) from app_message where `union` LIKE '$id,%' OR `union` LIKE '%,$id' group by `union`) order by id desc limit $from,$size");
        return $result;
    }
    /**
     * 根据搜索条件获取总条数
     */
    public function getCounts($id)
    {
        $result = $this->query("SELECT COUNT(*) AS num from app_message where id in (select max(id) from app_message where fromid = $id  or toid = $id group by fromid,toid)");
        return $result;
    }
    /**
     * 根据搜索条件获取列表信息
     */
    public function getOne($map)
    {
        $order = ['id' => 'desc'];
        $result = $this->where($map)->order($order)->find();
        return $result;
    }
    public function getlist($id1,$id2,$from = 0, $size = 5)
    {
        $result = $this->query("select * from app_message where (fromid = $id1 and toid = $id2 and states = 1) or (toid = $id1 and fromid = $id2 and states = 1) limit $from,$size");
        return $result;
    }
    public function getCount($id1,$id2)
    {
        $result = $this->query("SELECT COUNT(*) AS num from app_message where (fromid = $id1 and toid = $id2 and states = 1) or (toid = $id1 and fromid = $id2 and states = 1)");
        return $result;
    }
    /**
     * 根据搜索条件获取列表信息
     */
    public function getAlist($map, $from = 0, $size = 5)
    {
        $order = ['r.id' => 'desc'];
        $join1 = [
            ['app_member am1','am1.id = r.fromid'],
        ];
        $join2 = [
            ['app_member am2','am2.id = r.toid'],
        ];
        $result = $this->field('r.id,r.fromid,r.toid,r.content,r.addtime,am1.username as fromusername,am2.username as tousername')
            ->alias('r')
            ->join($join1)
            ->join($join2)
            ->where($map)
            ->order($order)
            ->limit($from, $size)
            ->select();

        return $result;
    }
    /**
     * 根据搜索条件获取总条数
     */
    public function getAcount($map)
    {
        $join1 = [
            ['app_member am1','am1.id = r.fromid'],
        ];
        $join2 = [
            ['app_member am2','am2.id = r.toid'],
        ];
        $result = $this->alias('r')
            ->join($join1)
            ->join($join2)
            ->where($map)
            ->count();

        return $result;
    }
}